import { getUserInfoApi, login } from '@/api/user'
import { getToken, removeToken, setToken } from '@/utils/auth'

const state = {
  token: getToken() || '',
  userInfo: ''
}
const mutations = {
  setToken(state, val) {
    state.token = val
    setToken(val)
  },
  removeToken(state) {
    state.token = ''
    removeToken()
  },
  setUserInfo(state, val) {
    state.userInfo = val
  },
  removeUserInfo(state) {
    state.userInfo = ''
  }
}
const actions = {
  async getToken({ commit }, value) {
    const {
      data: { token }
    } = await login(value)
    commit('setToken', token)
    // console.log(token)
  },
  logout({ commit }) {
    commit('removeToken')
    commit('removeUserInfo')
  },
  async getUserInfo({ commit }) {
    const { data } = await getUserInfoApi()
    /*     console.log(data) */
    commit('setUserInfo', data)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
